package com.cn.springcloudclient.controller;

import com.cn.springcloudclient.model.URole;
import com.cn.springcloudclient.service.inter.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.lang.model.util.Elements;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.Document;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static jdk.nashorn.internal.objects.Global.print;

@RestController
public class TestController {
    @Value("${server.port}")
    String port;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DemoService demoService;

    @RequestMapping("/hi")
    public String home() {

        return "client: " + ",i am from port:" + port;
    }
    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest request){
        HttpSession seseion = request.getSession();
        String id = seseion.getId();
        System.out.println("clientsession:"+id);
        return id;
    }
    @RequestMapping("/getUsers")
    public List<Map<String, Object>> getDbType(HttpServletRequest request){
        String dt = request.getParameter("dt");
        System.out.println(dt);
        String sql = "select * from wx_menu";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            Set<Map.Entry<String, Object>> entries = map.entrySet( );
            if(entries != null) {
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator( );
                while(iterator.hasNext( )) {
                    Map.Entry<String, Object> entry =(Map.Entry<String, Object>) iterator.next( );
                    Object key = entry.getKey( );
                    Object value = entry.getValue();
                    System.out.println(key+":"+value);
                }
            }
        }
        return list;
    }

    @RequestMapping("/demo")
    public String demo(String id){
        URole uRole = null;
        try{
            uRole = demoService.selectByPrimaryKey(Long.valueOf(id));
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return uRole.toString();
    }



}
